package ar.com.mooral.activities;



import java.util.Date;
import java.util.Hashtable;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import ar.com.mooral.crohn.Crohn;
import ar.com.mooral.crohn.Synchronizer;
import ar.com.mooral.crohn.application.CrohnContext;
import ar.com.mooral.data.DatabaseHelper;
import ar.com.mooral.data.Result;


public class Login extends CrohnActivity{

	
	//private SharedPreferences settings;
	private Context context;
	
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
    	
    	//this.settings = this.getSharedPreferences(Crohn.PREFS_NAME, 0);
    	
    	context = this.getApplicationContext();
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

    	((EditText) findViewById(R.id.txtLoginUsername)).setText(CrohnContext.getUsername());
    	((EditText) findViewById(R.id.txtLoginPassword)).setText(CrohnContext.getPassword());

        
        // INGRESAR
		Button btnLoginIngresar = (Button) findViewById(R.id.btnLoginIngresar);
		btnLoginIngresar.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	
		    	
		    	// get data entered by the user
		    	EditText txtLoginUsername = (EditText) findViewById(R.id.txtLoginUsername);
		    	EditText txtLoginPassword = (EditText) findViewById(R.id.txtLoginPassword);
		    	
		    	Synchronizer sync = new Synchronizer(context);
		    	Result r = sync.doLogin(txtLoginUsername.getText().toString(), txtLoginPassword.getText().toString());
		    	
		    	if (r.hasError()) {
		    		showMessage(r);
		    	} else {
		    		

		            // If no username is set, ask for one, otherwise go straight to the menu 
		            boolean dbInitialize = context.getSharedPreferences(Crohn.PREFS_NAME, 0).getBoolean("dbInitialize", false);
		            String username = context.getSharedPreferences(Crohn.PREFS_NAME, 0).getString("username", "");
		            String password = context.getSharedPreferences(Crohn.PREFS_NAME, 0).getString("password", "");
		            String patientCode = context.getSharedPreferences(Crohn.PREFS_NAME, 0).getString("patientCode", "");
		            

		            // Check if we need to initialize database
		            if (!dbInitialize) {
		            	
		            	Context context = getApplicationContext();
		            	CharSequence text = "Esta operacion puede tomar unos segundos. Aguarde por favor...";
		            	int duration = Toast.LENGTH_SHORT;

		            	Toast toast = Toast.makeText(context, text, duration);
		            	toast.show();

		            	
		            	DatabaseHelper dbs[] = sync.getAllDatabasesHelperInstances();
		            	for (int i = 0; i< dbs.length; i++) {
		            		dbs[i].create();
		            	}


		            	// receive updates
		            	sync.receiveUpdates();
		            	
		            	
		        		SharedPreferences.Editor editor = context.getSharedPreferences(Crohn.PREFS_NAME, 0).edit();
		        		editor.putBoolean("dbInitialize", true);
		        		editor.commit();
		        		
						Intent intent = new Intent();
						intent.setComponent(new ComponentName(Main.class.getPackage().getName(), Main.class.getName()));
						startActivity(intent);
		        		
		            } else {
		            	
		            	// receive updates
		            	sync.receiveUpdates();
		            
			    		CrohnContext.setUsername(txtLoginUsername.getText().toString());
			    		CrohnContext.setPassword(txtLoginPassword.getText().toString());
						Intent intent = new Intent();
						intent.setComponent(new ComponentName(Main.class.getPackage().getName(), Main.class.getName()));
						startActivity(intent);
						
		            }
		    	}
		    }
		   
		   
		});

		// CANCELAR
		Button btnLoginCancelar = (Button) findViewById(R.id.btnLoginCancelar);
		btnLoginCancelar.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	finish();
		    }	    	
		});

    }
    
}